#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
#include "imageChandra.h"
#include "scan.h"
#include "extract.h"

char *inputFiles[10];
char *outputDir;
short formats;
char verbose, count_only;
int numberOfFiles, numberOfImagesExtracted, numberOfFilesScanned, numberOfFilesSkipped, numberOfMinPossibleImages;

int ch_usage(void) {
	printf("%s", ch_helpstring);
	exit(-1);
}

int main (int argc, char **argv) {
	int c, i=0;
	// Get and set option flag variables
	while ((c = getopt(argc, argv, "hjpgbco:")) != -1) {
		switch(c) {
			case 'h':
				ch_usage();
			case 'o':
				outputDir = optarg;
				break;
			case 'j':
				formats |= GET_JPGS;
				break;
			case 'b':
				formats |= GET_BMPS;
				break;
			case 'g':
				formats |= GET_GIFS;
				break;
			case 'p':
				formats |= GET_PNGS;
				break;
			case 'c':
				count_only |= 0x01;
				break;
			case 'v':
				verbose |= 0x01;
				break;
			default:
				return 0;
		}
	}

	if (formats == 0)
		formats = 0x1111;

	numberOfFiles = argc-optind;
	if (numberOfFiles == 0)
		ch_usage();

	ch_fileinfo *fileinfo;
	fileinfo = (ch_fileinfo *)calloc(numberOfFiles, sizeof(ch_fileinfo));
	
	// Load ch_fileinfo structures with filenames...
	for (i=0;i<(argc-optind);i++) {
		strncpy((fileinfo+i)->filename, argv[optind+i], 511);
	}

	// First pass (scan)
	for(i=0;i<numberOfFiles;i++) {
		printf("Starting heuristic scan on file: '%s'...\n", (fileinfo+i)->filename);
		ch_scan((fileinfo+i), formats);
		if (!count_only) {
			printf("Starting extraction..\n");
		}
	}
	/*
	fileinfo's should now have filled fields...
	(whether file was found, number of images found etc).. (see  definition of struct ch_fileinfo).
	Now must filter through structures and count how many were skipped (may as well do this now to save
	a pointless repeat of this loop), more importantly must decide to scan those structures that imply they represent
	files that contain images... (as found by ch_scan).
	 
	If count_only is set, don't extract. Just print image count possibilities.
	*/
	printf("debug");
	for(i=0;i<numberOfFiles;i++) {
		if ((fileinfo+i)->could_find_file == 'n') {
			numberOfFilesSkipped++;
		}

		else if ((fileinfo+i)->could_find_file == 'y') {
			if ((fileinfo+i)->image_count == 0) { // no images found...
				printf("\nNo images found in '%s'\n", (fileinfo+i)->filename);
				//printf("\n%d\n", *((fileinfo+i)->jpg_header_markers));
			}

			if ((fileinfo+i)->image_count > 0) {
				printf("%d possible images found in '%s'", (fileinfo+i)->filename);
				// extract here...
				// ch_extract((fileinfo+i), &outputDir)
				numberOfImagesExtracted++;
			}
			numberOfMinPossibleImages += (fileinfo+i)->possible_jpgs + (fileinfo+i)->possible_pngs + (fileinfo+i)->possible_gifs;
		}
	}

	printf("\n--------------------------\n%d\n", fileinfo->jpg_header_markers);
	printf("Finished with %d files scanned, %d files skipped,\n", numberOfFilesScanned, numberOfFilesSkipped);
	printf("and a total of %d images extracted out of a possible minimum of %d.\n", numberOfImagesExtracted, numberOfMinPossibleImages);
	return 0;
}